package sz.szsmk.citizencard.ui.base;

import android.app.Activity;
import android.content.Context;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.dialog.NetRequestTipsDialog;
import sz.szsmk.citizencard.ioc.Compontent.AppCompontent;
import com.trello.rxlifecycle.LifecycleTransformer;
import com.trello.rxlifecycle.components.support.RxFragment;

import javax.inject.Inject;


/**
 * 涉及dagger业务逻辑继承此类。如果没有业务逻辑，继承普通的V4Fragment
 *@author sinner
 *@time 2017/3/20 下午2:56
 *@mail wangzhongfu@xwtec.cn
 */
public abstract class IBaseFragment<T extends IBaseContact.IBasePresenter,V extends ViewDataBinding> extends RxFragment implements IBaseContact.IBaseView {

    @Inject
    protected T mPresenter;
    protected Activity mActivity;

    protected V dataBinding;
    protected Context mContext;
    //缓存Fragment view
    private View mRootView;
    private boolean mIsMulti = false;
    private NetRequestTipsDialog mNetRequestTipsDialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (mRootView == null) {
            mActivity = getActivity();
            mRootView = inflater.inflate(attachLayoutRes(), null);
            dataBinding = DataBindingUtil.bind(mRootView);
            initInjector();
            if(mPresenter != null){
                mPresenter.attachView(this);
            }
            initViews();
        }
        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null) {
            parent.removeView(mRootView);
        }

       return mRootView;
    }


    /**
     * 此方法在onCreateView后立刻执行
     * @param view
     * @param savedInstanceState
     */
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if(mPresenter != null){
            mPresenter.attachView(this);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getUserVisibleHint() && mRootView != null && !mIsMulti) {
            mIsMulti = true;
            updateViews();
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser && isVisible() && mRootView != null && !mIsMulti) {
            mIsMulti = true;
            updateViews();
        } else {
            super.setUserVisibleHint(isVisibleToUser);
        }
    }


    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.<T>bindToLifecycle();
    }

    /**
     * 获取 ApplicationComponent
     *
     * @return ApplicationComponent
     */
    protected AppCompontent getAppComponent() {
        return DaggerApplication.getAppComponent();

    }

    /**
     * 绑定布局文件
     * @return  布局文件ID
     */
    protected abstract int attachLayoutRes();

    /**
     * Dagger 注入
     */
    protected abstract void initInjector();

    /**
     * 初始化视图控件
     */
    protected abstract void initViews();

    /**
     * 更新视图控件
     */
    protected abstract void updateViews();

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(mPresenter != null){
            mPresenter.detachView();
        }
    }


}
